home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.5 / Examples / Icon / ConvertMagicWBIcons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-30  |  7.4 KB  |  362 lines

  1. /*
  2.  * $Id$
  3.  *
  4.  * :ts=4
  5.  *
  6.  * COPYRIGHT:
  7.  *
  8.  *   Unless otherwise noted, all files are Copyright (c) 1999 Amiga, Inc.
  9.  *   All rights reserved.
  10.  *
  11.  * DISCLAIMER:
  12.  *
  13.  *   This software is provided "as is". No representations or warranties
  14.  *   are made with respect to the accuracy, reliability, performance,
  15.  *   currentness, or operation of this software, and all use is at your
  16.  *   own risk. Neither Amiga nor the authors assume any responsibility
  17.  *   or liability whatsoever with respect to your use of this software.
  18.  *
  19.  */
  20.  
  21. #include <dos/dosextens.h>
  22. #include <dos/dosasl.h>
  23. #include <dos/rdargs.h>
  24.  
  25. #include <exec/memory.h>
  26.  
  27. #include <workbench/workbench.h>
  28.  
  29. #include <clib/exec_protos.h>
  30. #include <clib/utility_protos.h>
  31. #include <clib/graphics_protos.h>
  32. #include <clib/dos_protos.h>
  33.  
  34. #include <pragmas/exec_sysbase_pragmas.h>
  35. #include <pragmas/utility_pragmas.h>
  36. #include <pragmas/graphics_pragmas.h>
  37. #include <pragmas/dos_pragmas.h>
  38.  
  39. #include <string.h>
  40.  
  41. /****************************************************************************/
  42.  
  43. extern struct Library * SysBase;
  44. extern struct Library * DOSBase;
  45. extern struct Library * UtilityBase;
  46. extern struct Library * GfxBase;
  47. extern struct Library * IconBase;
  48.  
  49. /****************************************************************************/
  50.  
  51. #include <workbench/icon.h>
  52. #include <clib/icon_protos.h>
  53. #include <pragmas/icon_pragmas.h>
  54.  
  55. /****************************************************************************/
  56.  
  57. #define OK (0)
  58. #define SAME (0)
  59.  
  60. /****************************************************************************/
  61.  
  62. #define MAX_PATH_LEN 1024
  63.  
  64. /****************************************************************************/
  65.  
  66. #define FIB_IS_DRAWER(fib)    ((fib)->fib_DirEntryType > 0)
  67. #define FIB_IS_FILE(fib)    ((fib)->fib_DirEntryType < 0)
  68.  
  69. /****************************************************************************/
  70.  
  71. UBYTE MagicWBPalette[8 * 3] =
  72. {
  73.     0x96,0x96,0x96,
  74.     0x00,0x00,0x00,
  75.     0xFF,0xFF,0xFF,
  76.     0x3D,0x65,0xA2,
  77.     0x79,0x79,0x79,
  78.     0xAE,0xAE,0xAE,
  79.     0xAA,0x92,0x7D,
  80.     0xFF,0xAA,0x96
  81. };
  82.  
  83. /****************************************************************************/
  84.  
  85. VOID
  86. ImageToBitMap(
  87.     struct Image *    image,
  88.     struct BitMap *    bitMap)
  89. {
  90.     PLANEPTR plane;
  91.     LONG pageSize;
  92.     LONG i;
  93.  
  94.     memset(bitMap,0,sizeof(*bitMap));
  95.  
  96.     bitMap->BytesPerRow    = RASSIZE(image->Width,1);
  97.     bitMap->Rows        = image->Height;
  98.     bitMap->Depth        = image->Depth;
  99.  
  100.     pageSize = RASSIZE(image->Width,image->Height);
  101.     plane = (PLANEPTR)image->ImageData;
  102.  
  103.     for(i = 0 ; i < image->Depth ; i++)
  104.     {
  105.         bitMap->Planes[i] = plane;
  106.  
  107.         plane += pageSize;
  108.     }
  109. }
  110.  
  111. /****************************************************************************/
  112.  
  113. int
  114. main(int argc,char **argv)
  115. {
  116.     struct RDArgs * rda = NULL;
  117.     struct AnchorPath * ap;
  118.     STRPTR buffer = NULL;
  119.     UBYTE * imageBuffer = NULL;
  120.     UBYTE * imageBuffer1;
  121.     UBYTE * imageBuffer2;
  122.     int result = RETURN_FAIL;
  123.     STRPTR * names = NULL;
  124.     BOOL matched = FALSE;
  125.     LONG error = OK;
  126.     STRPTR str;
  127.  
  128.     if(IconBase->lib_Version < 44)
  129.     {
  130.         Printf("Could not open icon.library V44\n");
  131.         goto out;
  132.     }
  133.  
  134.     rda = ReadArgs("FILES/A/M",(LONG *)&names,NULL);
  135.     if(rda == NULL)
  136.     {
  137.         error = IoErr();
  138.         goto out;
  139.     }
  140.  
  141.     ap = AllocVec(sizeof(*ap) + MAX_PATH_LEN,MEMF_ANY);
  142.     if(ap == NULL)
  143.     {
  144.         error = ERROR_NO_FREE_STORE;
  145.         goto out;
  146.     }
  147.  
  148.     buffer = AllocVec(MAX_PATH_LEN,MEMF_ANY);
  149.     if(buffer == NULL)
  150.     {
  151.         error = ERROR_NO_FREE_STORE;
  152.         goto out;
  153.     }
  154.  
  155.     while((error == OK) && (str = (*names++)) != NULL)
  156.     {
  157.         memset(ap,0,sizeof(*ap));
  158.  
  159.         ap->ap_BreakBits = SIGBREAKF_CTRL_C;    
  160.         ap->ap_Strlen = MAX_PATH_LEN;
  161.  
  162.         matched = TRUE;
  163.  
  164.         error = MatchFirst(str,ap);
  165.         if(error == OK)
  166.         {
  167.             if(FIB_IS_DRAWER(&ap->ap_Info))
  168.             {
  169.                 ap->ap_Flags |= APF_DODIR;
  170.  
  171.                 error = MatchNext(ap);
  172.                 ap->ap_Flags &= ~APF_DIDDIR;
  173.             }
  174.  
  175.             while(error == OK)
  176.             {
  177.                 int len;
  178.  
  179.                 strcpy(buffer,ap->ap_Buf);
  180.                 len = strlen(buffer);
  181.  
  182.                 error = MatchNext(ap);
  183.  
  184.                 if(len < strlen(".info") || Stricmp(&buffer[len - strlen(".info")],".info") != SAME)
  185.                 {
  186.                     struct DiskObject * icon;
  187.                     LONG why;
  188.  
  189.                     Printf("Getting icon for \"%s\"... ",buffer);
  190.                     Flush(Output());
  191.  
  192.                     icon = GetIconTags(buffer,
  193.                         ICONA_ErrorCode,&why,
  194.                     TAG_DONE);
  195.  
  196.                     if(icon != NULL)
  197.                     {
  198.                         struct Image * image1 = (struct Image *)icon->do_Gadget.GadgetRender;
  199.                         struct Image * image2 = (struct Image *)icon->do_Gadget.SelectRender;
  200.                         LONG isPaletteMapped;
  201.  
  202.                         IconControl(icon,
  203.                             ICONCTRLA_IsPaletteMapped,&isPaletteMapped,
  204.                         TAG_DONE);
  205.  
  206.                         if(image1->Depth >= 3 && !isPaletteMapped)
  207.                         {
  208.                             struct RastPort rp;
  209.                             struct BitMap bm;
  210.                             LONG pixelsPerImage;
  211.                             int x,y;
  212.                             UBYTE * p;
  213.  
  214.                             pixelsPerImage = (LONG)image1->Width * (LONG)image1->Height;
  215.  
  216.                             FreeVec(imageBuffer);
  217.                             imageBuffer = AllocVec(2 * pixelsPerImage,MEMF_ANY);
  218.                             if(imageBuffer == NULL)
  219.                             {
  220.                                 Printf("\n");
  221.                                 error = ERROR_NO_FREE_STORE;
  222.                                 goto out;
  223.                             }
  224.  
  225.                             imageBuffer1 = imageBuffer;
  226.                             if(image2 != NULL)
  227.                             {
  228.                                 imageBuffer2 = imageBuffer + pixelsPerImage;
  229.                                 image2->Depth = 3;
  230.                             }
  231.                             else
  232.                             {
  233.                                 imageBuffer2 = NULL;
  234.                             }
  235.  
  236.                             Printf("converting... ");
  237.                             Flush(Output());
  238.  
  239.                             image1->Depth = 3;
  240.  
  241.                             InitRastPort(&rp);
  242.                             rp.BitMap = &bm;
  243.  
  244.                             ImageToBitMap(image1,&bm);
  245.  
  246.                             p = imageBuffer1;
  247.                             for(y = 0 ; y < image1->Height ; y++)
  248.                             {
  249.                                 for(x = 0 ; x < image1->Width ; x++)
  250.                                     (*p++) = ReadPixel(&rp,x,y);
  251.                             }
  252.  
  253.                             if(image2 != NULL)
  254.                             {
  255.                                 ImageToBitMap(image2,&bm);
  256.     
  257.                                 p = imageBuffer2;
  258.                                 for(y = 0 ; y < image1->Height ; y++)
  259.                                 {
  260.                                     for(x = 0 ; x < image1->Width ; x++)
  261.                                         (*p++) = ReadPixel(&rp,x,y);
  262.                                 }
  263.                             }
  264.  
  265.                             IconControl(icon,
  266.                                 ICONCTRLA_SetWidth,            image1->Width,
  267.                                 ICONCTRLA_SetHeight,        image1->Height,
  268.                                 ICONCTRLA_SetAspectRatio,    PACK_ICON_ASPECT_RATIO(1,1),
  269.                                 ICONCTRLA_SetImageData1,    imageBuffer1,
  270.                                 ICONCTRLA_SetPaletteSize1,    8,
  271.                                 ICONCTRLA_SetPalette1,        MagicWBPalette,
  272.                                 ICONA_ErrorCode,            &why,
  273.                             TAG_DONE);
  274.  
  275.                             if(why != OK)
  276.                             {
  277.                                 Printf("\n");
  278.                                 goto out;
  279.                             }
  280.  
  281.                             if(image2 != NULL)
  282.                             {
  283.                                 IconControl(icon,
  284.                                     ICONCTRLA_SetImageData2,    imageBuffer2,
  285.                                     ICONCTRLA_SetPaletteSize2,    8,
  286.                                     ICONCTRLA_SetPalette2,        MagicWBPalette,
  287.                                     ICONA_ErrorCode,            &why,
  288.                                 TAG_DONE);
  289.  
  290.                                 if(why != OK)
  291.                                 {
  292.                                     Printf("\n");
  293.                                     goto out;
  294.                                 }
  295.                             }
  296.  
  297.                             Printf("writing it back... ");
  298.                             Flush(Output());
  299.     
  300.                             if(PutIconTags(buffer,icon,
  301.                                 ICONA_ErrorCode,&why,
  302.                                 ICONPUTA_NotifyWorkbench,TRUE,
  303.                             TAG_DONE))
  304.                             {
  305.                                 Printf("ok.\n");
  306.                             }
  307.                         }
  308.                         else
  309.                         {
  310.                             Printf("not a MagicWB icon.\n");
  311.                         }
  312.  
  313.                         FreeDiskObject(icon);
  314.                     }
  315.  
  316.                     if(why != OK)
  317.                     {
  318.                         UBYTE errorString[100];
  319.                         int len;
  320.  
  321.                         Fault(why,NULL,errorString,sizeof(errorString));
  322.  
  323.                         len = strlen(errorString);
  324.                         while(len > 0 && errorString[len-1] == '\n')
  325.                             errorString[--len] = '\0';
  326.  
  327.                         Printf("failed (%s).\n",errorString);
  328.                     }
  329.                 }
  330.             }
  331.         }
  332.  
  333.         if(error == ERROR_NO_MORE_ENTRIES)
  334.             error = OK;
  335.  
  336.         MatchEnd(ap);
  337.         matched = FALSE;
  338.     }
  339.  
  340.     if(error == OK)
  341.         result = RETURN_OK;
  342.     else if (error == ERROR_BREAK)
  343.         result = RETURN_WARN;
  344.     else
  345.         result = RETURN_ERROR;
  346.  
  347.  out:
  348.  
  349.     if(matched)
  350.         MatchEnd(ap);
  351.  
  352.     FreeVec(ap);
  353.     FreeVec(buffer);
  354.     FreeVec(imageBuffer);
  355.     FreeArgs(rda);
  356.  
  357.     if(error != OK)
  358.         PrintFault(error,FilePart(argv[0]));
  359.  
  360.     return(result);
  361. }
  362.